from django.contrib import admin
from django.core.cache import cache
from app.models import IndexNewsBanner,IndexBottomCarousel,IndexTopCarousel,IndexVedioBanner,ReceiverEmail,TopicType,Subtitle,SubtitleImage,SubtitleVideo
# Register your models here.


class BaseModelAdmin(admin.ModelAdmin):
    def save_model(self, request, obj, form, change):
        """新增或更新时调用"""
        # 调用ModelAdmin中save_model来实现更新或新增
        super().save_model(request, obj, form, change)

        # 附加操作: 清除首页缓存
        cache.delete('index_page_data')

    def delete_model(self, request, obj):
        """删除数据时调用"""
        # 调用ModelAdmin中delete_model来实现删除操作
        super().delete_model(request, obj)
        # 附加操作: 清除首页缓存
        cache.delete('index_page_data')


class IndexNewsBannerAdmin(BaseModelAdmin):
    """首页新闻展示模型管理类"""
    list_display = ('language','title','image','index','update_time')
    list_display_links = ('language','title','image','index')
    list_per_page = 10
    search_fields = ('language','title','content','index')


class IndexBottomCarouselAdmin(BaseModelAdmin):
    """首页底部轮播图模型管理类"""
    list_display = ('image','index')
    list_display_links = ('index',)
    list_per_page = 10
    search_fields = ('index',)


class IndexTopCarouselAdmin(BaseModelAdmin):
    """首页顶部轮播图模型管理类"""
    list_display = ('big_title','big_title_en','small_title','small_title_en','image','index')
    list_display_links = ('big_title','big_title_en','small_title','small_title_en','index')
    list_per_page = 10
    search_fields = ('big_title','big_title_en','small_title','small_title_en','index')


class IndexVedioBannerAdmin(BaseModelAdmin):
    """首页视频展示模型管理类"""
    list_display = ('title','logo','image','vedio','index')
    list_display_links = ('title','index')
    list_per_page = 10
    search_fields = ('title','index')



class ReceiverEmailAdmin(admin.ModelAdmin):
    """接收邮箱地址模型管理类"""
    list_display = ('email','create_time')


# 产品中心模型管理类
class TopicTypeAdmin(admin.ModelAdmin):
    """主题类型模型管理类"""
    list_display = ('topic','topic_en','desc','desc_en','background_image','index')
    list_display_links = ('topic','topic_en','desc','desc_en','index')
    search_fields = ('topic','topic_en','desc','desc_en','index')


class SubtitleAdmin(admin.ModelAdmin):
    """子标题模型管理类"""
    list_display = ('topic','title','title_en','logo','desc','desc_en','index')
    list_display_links = ('title','title_en','logo','desc','desc_en','index')
    search_fields = ('title','title_en','desc','desc_en','index')


class SubtitleImageAdmin(admin.ModelAdmin):
    """子标题图片模型管理类"""
    list_display = ('subtitle','image','index')
    list_display_links = ('index',)
    search_fields = ('index',)


class SubtitleVideoAdmin(admin.ModelAdmin):
    """子标题视频模型管理类"""
    list_display = ('subtitle','video','index')
    list_display_links = ('index',)
    search_fields = ('index',)

admin.site.register(IndexNewsBanner,IndexNewsBannerAdmin)  # 首页新闻
admin.site.register(IndexBottomCarousel,IndexBottomCarouselAdmin)  # 首页底部轮播
admin.site.register(IndexTopCarousel,IndexTopCarouselAdmin)  # 首页顶部轮播
admin.site.register(IndexVedioBanner,IndexVedioBannerAdmin)  # 首页视频部分
admin.site.register(ReceiverEmail,ReceiverEmailAdmin)  # 首页邮箱
admin.site.register(TopicType,TopicTypeAdmin)  # 产品页主题类型
admin.site.register(Subtitle,SubtitleAdmin)  # 产品页子标题
admin.site.register(SubtitleImage,SubtitleImageAdmin)  # 产品页子标题图片
admin.site.register(SubtitleVideo,SubtitleVideoAdmin)  # 产品页子标题视频









